package com.sunday.fangdaifu.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;

import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/7/19.
 */

public class SelectDirectionWindow extends PopupWindow {

    private Context mContext;
    private String direction;
    private OnSelectDirection onSelectDirection;

    public SelectDirectionWindow(Context context){
        mContext=context;
        View view= LayoutInflater.from(mContext).inflate(R.layout.layout_select_direction_window,null);
        setContentView(view);
        ButterKnife.bind(this,view);
        int appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
        this.setWidth(appWidth);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setOnDismissListener(new PoponDismissListener());
        this.setBackgroundDrawable(new BitmapDrawable());
        //this.setAnimationStyle(R.style.Theme_AppCompat_Dialog_Alert);
        RoundMenusView roundMenusView= (RoundMenusView) view.findViewById(R.id.menu_view);
        TextView btnConfirm= (TextView) view.findViewById(R.id.btn_confirm);
        roundMenusView.setOnSelectListener(new RoundMenusView.OnSelectListener() {
            @Override
            public void select(String menuItem) {
                direction=menuItem;
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(direction)){
                    ToastUtils.showToast(mContext,"请选择方位");
                    return;
                }
                if (onSelectDirection!=null){
                    onSelectDirection.selectDirection(direction);
                    dismiss();
                }

            }
        });
    }


    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            backgroundAlpha(0.5f);
        } else {
            this.dismiss();
        }
    }

    public void setOnSelectDirection(OnSelectDirection onSelectDirection) {
        this.onSelectDirection = onSelectDirection;
    }

    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class PoponDismissListener implements OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
            dismiss();
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    public interface OnSelectDirection{
        void selectDirection(String direction);
    }


}
